<template>
  <el-dialog :model-value="props.show" :title="`${title}会员等级`" width="440px" :before-close="handleClose">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="auto">
      <el-form-item label="等级名称" prop="name">
        <el-input style="width: 80%;" :maxlength="10" show-word-limit v-model="form.name" placeholder="请输入会员等级名称"></el-input>
      </el-form-item>
      <el-form-item label="获取条件" prop="acquisitionCondition">
        <div class="flex">
          <span>累计积分达到</span>
          <el-input :disabled="detail.levelType === 0" style="width: 100px;margin: 0 8px;" v-model.number="form.acquisitionCondition"
            placeholder="请输入"></el-input>
          <span>分，升级为此等级</span>
        </div>
      </el-form-item>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button :loading="loading" @click="handleClose">取消</el-button>
        <el-button :loading="loading" type="primary" @click="confirm(formRef)">
          确定
        </el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import { useCommonData } from '../Hooks/index.js'
import { addMemberLevel, updateMemberLevel, } from '@/api/Member/index.js';
const props = defineProps({
  show: {
    type: Boolean,
    default: false,
  },
  detail: {
    type: Object,
    default: () => ({ }),
  },
});
const isEdit = computed(() => {
  return Object.keys(props.detail).length > 0;
});
const title = computed(() => {
  return isEdit.value ? '编辑' : '新增';
});
const { seachTableRef } = useCommonData()

const rules = ref({
  name: [
    { required: true, message: '请输入会员等级名称', trigger: 'blur' },
  ],
  acquisitionCondition: [
    { required: true, message: '请输入获取条件', trigger: 'blur' },
  ],
});
const form = ref({
  name: '',
  acquisitionCondition: ''
});
watch(()=>props.detail,(newVal)=>{
  if(newVal.id){
    form.value.name = newVal.levelName
    form.value.acquisitionCondition = newVal.levelIntegralNum
  }
},{
  deep: true
})

const loading = ref(false);
const formRef = ref();
const confirm = async (formEl) => {
    if (!formEl) return;
    formEl.validate(async (valid) => {
      try {
        loading.value = true;
        if (!valid) return;
        const params = {
          levelName: form.value.name,
          levelIntegralNum: form.value.acquisitionCondition,
        };
        if (isEdit.value) {
          await updateMemberLevel(props.detail.id,params)
        }else{
          await addMemberLevel(params)
        }
        ElMessage.success(isEdit.value ? '编辑成功' : '操作成功');
        seachTableRef.value && seachTableRef.value.handlerSearch();
        handleClose();
      }finally{
        loading.value = false;
      }
    });
}

const emit = defineEmits(['close']);
const handleClose = () => {
  if (formRef.value) {
    formRef.value.resetFields();
    form.value = {
      name: '',
      acquisitionCondition: ''
    };
  }
  emit('close', false);
};
</script>

<style lang="less" scoped>
.flex {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>